# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


from PyQt4 import QtCore, QtGui
from ui_pubchemDialog import Ui_PubchemDialog

import errorHandler
from pubchem import *

class PubchemDialog(QtGui.QDialog):

	maxCompoundLength = 20

	def __init__(self, controller, compoundClass):
		QtGui.QDialog.__init__(self)

		self.controller = controller
		self.compoundClass = compoundClass

		self.res = None

		# Set up the user interface from Designer.
		self.ui = Ui_PubchemDialog()
		self.ui.setupUi(self)

		self.connect(self.ui.searchPushButton, QtCore.SIGNAL("clicked()"), self.search)
		self.connect(self.ui.resultsListWidget, QtCore.SIGNAL("itemDoubleClicked (QListWidgetItem *)"), self.returnCompound)
		self.connect(self.ui.buttonBox, QtCore.SIGNAL("rejected()"), self.close)
		self.connect(self.ui.buttonBox, QtCore.SIGNAL("accepted()"), self.returnCompound)


	def search(self):
		self.ui.resultsListWidget.clear()
		
		compound = str(self.ui.nameLineEdit.text())

		errorHandler.startHandler()
		
		self.res = searchPubChem(compound)

		if not self.res:
			errorHandler.endHandler()
			msgDialog = QtGui.QMessageBox(QtGui.QMessageBox.Information, "No Resutlts", "PubChem returned no results for your search", QtGui.QMessageBox.Ok)
			msgDialog.exec_()
			return

		for val in self.res.values():
			self.ui.resultsListWidget.addItem(val)

		errorHandler.endHandler()


	def returnCompound(self, item=None):
		if not item or type(item) != QtGui.QListWidgetItem:
			item = self.ui.resultsListWidget.currentItem()

		if not item:
			self.close()
			return
		
		name = str(item.text())

		cid = None
		for key in self.res:
			if self.res[key] == name:
				cid = key
				break

		errorHandler.startHandler()
		
		data = PubChemObj(cid)
		sdf = data.getSDF()

		end = sdf.find(">")
		molfile = sdf[0:end]

		begin = sdf.find("InChI")
		end = sdf.find("\n", begin)
		inchi = sdf[begin:end]

		begin = sdf.find("<PUBCHEM_MOLECULAR_FORMULA>")
		begin = sdf.find("\n", begin) + 1
		end = sdf.find("\n", begin)
		formula = sdf[begin:end]

		begin = sdf.find("<PUBCHEM_IUPAC_NAME>")
		begin = sdf.find("\n", begin) + 1
		end = sdf.find("\n", begin)
		iupac = sdf[begin:end]

		compound = self.compoundClass()
		compound.name = name
		compound.molfile = molfile
		compound.formula = formula
		compound.IUPACname = iupac
		compound.inchi = inchi

		self.controller.setViewCurrentCompound(compound)

		errorHandler.endHandler()

		if self.isVisible():
			self.close()
